package com.czy.owner.ui.physicalorder;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.czy.owner.R;
import com.czy.owner.adapter.CommonTabLayoutAdapter;
import com.czy.owner.db.UserHelper;
import com.czy.owner.global.Constants;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.ui.BaseFragment;
import com.czy.owner.utils.MyLog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by wangyang on 2017/6/13 0013.
 */

public class DistributionKindActivity extends BaseActivity {
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.tabs)
    TabLayout mTabLayout;
    private List<String> titles;
    private CommonTabLayoutAdapter mFragmentAdapter;
    private List<BaseFragment> mFragments = new ArrayList<>();

    private int showType = 1;
   private boolean isShowTabLayout = true;
    private boolean isInstall = true;
    @Override
    protected int attachLayoutRes() {
        return R.layout.common_activity_tablayout;
    }

    @Override
    protected void initInjector() {
        showType = getIntent().getIntExtra("showType", 1);
        isShowTabLayout = getIntent().getBooleanExtra("isShowTabLayout",true);
        isInstall = getIntent().getBooleanExtra("isInstall",true);
    }

    @Override
    protected void initViews() {
        if(!isShowTabLayout){
            getToolbarTitle().setText("联系人信息");
        }else {
            getToolbarTitle().setText(R.string.distribution_kind);
        }

        initViewPager();
    }

    @Override
    protected void updateViews() {

    }


    private void initViewPager() {
        if(!isShowTabLayout){
            mTabLayout.setVisibility(View.GONE);
        } else {
            mTabLayout.setVisibility(View.VISIBLE);
        }
        addTabTitle();
        addTabFragment();
        mFragmentAdapter = new CommonTabLayoutAdapter(getSupportFragmentManager(), mFragments, titles);
//        //给ViewPager设置适配器
        viewPager.setAdapter(mFragmentAdapter);
        viewPager.setOffscreenPageLimit(titles.size());
        if(isShowTabLayout){
            viewPager.setCurrentItem(showType == 1 || "0".equals(getIntent().getStringExtra("goodsId")) ? 1 : 0);
        }
        //将TabLayout和ViewPager关联起来。
        mTabLayout.setupWithViewPager(viewPager);

    }

    private void addTabTitle() {
        titles = new ArrayList<>();
        titles.add("配送到店");
        titles.add("配送到家");
        for (int i = 0; i < titles.size(); i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(i)));
        }

    }


    private void addTabFragment() {
        SelectServiceStoreFragment selectServiceStoreFragment = new SelectServiceStoreFragment();
        Bundle storeArgument = new Bundle();
        storeArgument.putBoolean("hideSearchBar",true);
        selectServiceStoreFragment.setArguments(storeArgument);

        AddressFragment addressFragment = new AddressFragment();
        Bundle addressArgument = new Bundle();
        addressArgument.putBoolean("needBack", true);
        addressFragment.setArguments(addressArgument);
        if(!isShowTabLayout){
            addressArgument.putBoolean("isFrom", true);
        }else {
            addressArgument.putBoolean("isFrom", false);
        }
        addressArgument.putBoolean("isInstall",isInstall);
        if(isShowTabLayout){
            mFragments.add(selectServiceStoreFragment);
        }
        mFragments.add(addressFragment);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
